package org.dipler.traffic;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class Engset extends Activity {

	Button btn;
	RadioButton rdbProb;
	RadioButton rdbTraffic;
	RadioButton rdbLines;
	EditText txtProb;
	EditText txtTraffic;
	EditText txtLines;
	EditText txtUsers;
	RadioGroup rdg;
	TextView lblResult;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.engset);

        setVariables();
        
        rdg.setOnCheckedChangeListener(new OnCheckedChangeListener(){

			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if(rdbTraffic.isChecked()){
					txtTraffic.setEnabled(false);
					txtProb.setEnabled(true);
					txtLines.setEnabled(true);
				}else if(rdbProb.isChecked()){
					txtTraffic.setEnabled(true);
					txtProb.setEnabled(false);
					txtLines.setEnabled(true);
				}else{
					txtTraffic.setEnabled(true);
					txtProb.setEnabled(true);
					txtLines.setEnabled(false);
				}
			}
        	
        });
        
        btn.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				int s = Integer.parseInt(txtUsers.getText().toString());
				if(rdbTraffic.isChecked()){
					int m = Integer.parseInt(txtLines.getText().toString());
					double pb = Double.parseDouble(txtProb.getText().toString());
					double a = Traffic.engset_a(s, m, pb, 0.01);
					lblResult.setText(getString(R.string.Traffic) + " = " + String.valueOf(a));
					txtTraffic.setText(String.valueOf(a));
				}else if(rdbProb.isChecked()){
					int m = Integer.parseInt(txtLines.getText().toString());
					double a = Double.parseDouble(txtTraffic.getText().toString());
					double pb = Traffic.engset_pb(a, s, m);
					lblResult.setText(getString(R.string.Probability) + " = " + String.valueOf(pb * 100) + "%");
					txtProb.setText(String.valueOf(pb));
				}else{ // lineas
					double a = Double.parseDouble(txtTraffic.getText().toString());
					double pb = Double.parseDouble(txtProb.getText().toString());
					int m = Traffic.engset_m(s, a, pb);
					lblResult.setText(getString(R.string.Lines) + " = " + String.valueOf(m));
					txtLines.setText(String.valueOf(m));
				}
			}
		});
        
        clear();
    }

	private void setVariables() {
		rdbProb = (RadioButton)findViewById(R.id.rdbProb);
        rdbTraffic = (RadioButton)findViewById(R.id.rdbTraffic);
        rdbLines = (RadioButton)findViewById(R.id.rdbLines);
        btn = (Button)findViewById(R.id.btnCalcEb);
        lblResult = (TextView) findViewById(R.id.lblResult);
        rdg = (RadioGroup) findViewById(R.id.rdg);
        txtLines = (EditText)findViewById(R.id.txtLines);
        txtProb = (EditText)findViewById(R.id.txtPb);
        txtTraffic = (EditText)findViewById(R.id.txtTraffic);
        txtUsers = (EditText)findViewById(R.id.txtUsers);
	}
	
	private void clear(){
		rdbProb.setChecked(true);
		txtTraffic.setText("0");
		txtProb.setText("0");
		txtLines.setText("0");
		txtUsers.setText("0");
	}
}
